package com.young.shop.api.service.impl;

import com.young.shop.api.dao.CommentMapper;
import com.young.shop.api.service.CommentService;
import com.young.shop.pojo.entity.Comment;
import com.young.shop.pojo.entity.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService{
    @Autowired
    CommentMapper commentMapper;
    @Override
    public List<Goods> inquiryByOrderId(Long orderId) {
        List<Goods> goods = commentMapper.selectGoodsByOrderId(orderId);
        List<Goods> goodsList = removeDuplication(goods);
        if (goodsList.size() == 0){
            throw new RuntimeException("查询评论商品失败");
        }
        return goodsList;
    }

    @Override
    public int commentGoods(Comment comment) {
        //将评论等级、评论内容插入商品评论详情表中，返回评论id（映射主键）
        commentMapper.insertComment(comment);
        System.out.println(comment);
        //将用户名、商品id、评论id插入商品评论关系表中，返回插入结果
        int i = commentMapper.insertRelation(comment);
        if (i == 0){
            throw new RuntimeException("评论商品失败");
        }
        return i;
    }







    //商品去重
    public List<Goods> removeDuplication(List<Goods> goodsList){
        for (int i= 0;i<goodsList.size()-1;i++){
            Integer id = goodsList.get(i).getId();
            for (int j=i+1;j<goodsList.size();j++){
                Integer id1 = goodsList.get(j).getId();
                if(id==id1){
                    goodsList.remove(j);
                    j--;
                }
            }
        }
            return goodsList;
    }
}
